package com.njmaomao.gj.pyot.domain.model;

import com.njmaomao.gj.pyot.common.ResponseCode;
import com.njmaomao.gj.pyot.common.exception.SystemBizFail;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.stream.Stream;

/**
 * @author hh
 * @description (trading_pay_record)表实体类
 * @date 2019-03-13 14:47:40
 */
@Entity
@Table(name = "wechat_pay_record")
public class PayRecord implements Serializable {

    private static final long serialVersionUID = -53532639552690078L;

    /**
     * 唯一标识
     */
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    /**
     * 订单号
     */
    @Column(name = "order_code")
    private String orderCode;

    /**
     * 交易流水号(可空,因为交易流水号在支付成功后方可获得)
     */
    @Column(name = "trade_no")
    private String tradeNo;

    /**
     * 实际支付金额,单位是分
     */
    @Column(name = "total_amount")
    private Integer totalAmount;

    /**
     * 货币类型
     */
    @Column(name = "currency")
    private String currency;

    @Column(name = "app_id")
    private String appId;

    /**
     * 收款商户号
     */
    @Column(name = "mch_id")
    private String mchId;

    /**
     * 买家ID openid、支付宝
     */
    @Column(name = "user_id")
    private String userId;

    /**
     * 会员系统用户ID
     */
    @Column(name = "customer_id")
    private Long customerId;

    /**
     * 支付平台(ALIPAY,WEIXIN,UNION_PAY)
     */
    @Column(name = "pay_style")
    private Integer payStyle;

    /**
     * 支付方式
     */
    @Column(name = "pay_mode")
    private String payMode;

    /**
     * 订单类型(1:订单系统的订单，2:内容订单，其他)
     */
    @Column(name = "order_type")
    private Integer orderType;

    /**
     * 来源系统
     */
    @Column(name = "from_system")
    private String fromSystem;

    /**
     * 状态:0待支付,1已支付,2已关闭
     */
    @Column(name = "status")
    private Integer status;

    /**
     * 支付平台通知支付系统订单支付结果，支付系统将状态通知到指定url
     */
    @Column(name = "callback_url")
    private String callbackUrl;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;

    /**
     * 支付时间,支付成功时回调的时间
     */
    @Column(name = "pay_time")
    private Date payTime;

    /**
     * 描述
     */
    @Column(name = "pay_desc")
    private String payDesc;

    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOrderCode() {
        return orderCode;
    }

    public void setOrderCode(String orderCode) {
        this.orderCode = orderCode;
    }

    public String getTradeNo() {
        return tradeNo;
    }

    public void setTradeNo(String tradeNo) {
        this.tradeNo = tradeNo;
    }

    public Integer getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(Integer totalAmount) {
        this.totalAmount = totalAmount;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Integer getPayStyle() {
        return payStyle;
    }

    public void setPayStyle(Integer payStyle) {
        this.payStyle = payStyle;
    }

    public String getPayMode() {
        return payMode;
    }

    public void setPayMode(String payMode) {
        this.payMode = payMode;
    }

    public Integer getOrderType() {
        return orderType;
    }

    public void setOrderType(Integer orderType) {
        this.orderType = orderType;
    }

    public String getFromSystem() {
        return fromSystem;
    }

    public void setFromSystem(String fromSystem) {
        this.fromSystem = fromSystem;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getCallbackUrl() {
        return callbackUrl;
    }

    public void setCallbackUrl(String callbackUrl) {
        this.callbackUrl = callbackUrl;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getPayTime() {
        return payTime;
    }

    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    public String getPayDesc() {
        return payDesc;
    }

    public void setPayDesc(String payDesc) {
        this.payDesc = payDesc;
    }

    /**判断是否需要回调
     * 如果callbackurl不为空，就需要回调
     * @return
     */
    public boolean necessaryCallback(){
        return StringUtils.isNotBlank(getCallbackUrl());
    }

    /**
     * 判断是否是已支付状态
     * @return
     */
    public boolean paid(){
        return getStatus() == Status.PAYED.getStatus();
    }

    public enum CurrencyType {
        CNY, USD,GBP,HKD,CHF,SGD,SEK,DKK,NOK,JPY,CAD,AUD,EUR,NZD,KRW,THB;
    }

    public enum Status{

        PAYING(0),PAYED(1),PAYCLOSE(2);

        private int status;

        Status(int status) {
            this.status = status;
        }

        public int getStatus() {
            return status;
        }

    }

    public enum OrderType {


        ORDER(1), CONTENTORDER(2), APPDEFINED(3), Errand(4);

        int type;

        OrderType(int val) {
            this.type = val;
        }

        public int getType() {
            return type;
        }

        public static OrderType get(Integer orderType) {
            switch (orderType) {
                case 1:
                    return ORDER;
                case 2:
                    return CONTENTORDER;
                case 3:
                    return APPDEFINED;
                case 4:
                    return Errand;
                default:
                    SystemBizFail.happen(ResponseCode.PAYTO.UNKNOW_ORDERTYPE);
                    return null;
            }
        }
    }

    public enum PayMode {
        ALIPAY_APP("ALIPAY_APP","支付宝APP"),ALIPAY_BARCODE("ALIPAY_BARCODE","支付宝条码"),ALIPAY_SCANCODE("ALIPAY_SCANCODE","支付宝扫码"),
        ALIPAY_WAP("ALIPAY_WAP","支付宝手机网页"),ALIPAY_WEBSITE("ALIPAY_WEBSITE","支付宝电脑网页"),
        ALIPAY_INTL_APP("ALIPAY_INTL_APP","支付宝国际APP"),ALIPAY_INTL_WAP("ALIPAY_INTL_WAP","支付宝国际手机网页"),ALIPAY_INTL_WEBSITE("ALIPAY_INTL_WEBSITE","支付宝国际网页"),
        ALIPAY_INTL_BARCODE("ALIPAY_INTL_BARCODE","支付宝国际条码"),ALIPAY_INTL_TQRCODE("ALIPAY_INTL_TQRCODE","支付宝国际扫码"),
        WECHAT_MICROPAY("MICROPAY", "付款码支付"),WECHAT_APP("APP", "APP支付"),WECHAT_JSAPI("JSAPI", "JSAPI支付");

        String mode;
        String description;
        PayMode(String mode, String description) {
            this.mode = mode;
            this.description = description;
        }

        public String getMode() {
            return mode;
        }

        public String getDescription() {
            return description;
        }

        /**
         *判断是不是支付宝国际到店付的支付方式
         * @param mode
         * @return
         */
        public static boolean isAliIntlInStorePayMode(String mode){
            return null != mode && Arrays.stream(getAliIntlInStorePayMode())
                    .filter(inStoreMode -> StringUtils.equals(inStoreMode,mode))
                    .count() > 0;
        }

        /**
         * 判断是否属于支付宝国际支付方式
         * @param mode
         * @return
         */
        public static boolean isAliIntlPayMode(String mode){
            return null != mode && Arrays.stream(getAliIntlPayMode())
                    .filter(inStoreMode -> StringUtils.equals(inStoreMode,mode))
                    .count() > 0;
        }

        public static PayMode get(String mode){
            PayMode[] payModes = PayMode.values();
            return Stream.of(payModes)
                    .filter(payMode -> StringUtils.equals(payMode.getMode(),mode))
                    .findFirst()
                    .orElse(null);
        }

        /**
         * 获取支付宝国际到店付的支付方式
         * @return
         */
        public static String[] getAliIntlInStorePayMode(){
            return new String[]{ALIPAY_INTL_BARCODE.getMode(),ALIPAY_INTL_TQRCODE.getMode()};
        }

        /**
         * 获取支付宝国际在线支付的支付方式
         * @return
         */
        public static String[] getAliIntlOnlinePayMode(){
            return new String[]{ALIPAY_INTL_APP.getMode(),ALIPAY_INTL_WAP.getMode(),ALIPAY_INTL_WEBSITE.getMode()};
        }

        /**
         * 获取支付宝国际支付方式
         * @return
         */
        public static String[] getAliIntlPayMode(){
            return new String[]{ALIPAY_INTL_APP.getMode(),ALIPAY_INTL_WAP.getMode(),ALIPAY_INTL_WEBSITE.getMode(),
                    ALIPAY_INTL_BARCODE.getMode(),ALIPAY_INTL_TQRCODE.getMode()};
        }

    }

}